<template>
  <el-dialog
    v-model="dialogVisible"
    title="确定班次"
    width="1000px"
    @close="closeDialog"
  >
    <el-form class="aceauForm" label-width="120px" :model="state.form">
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item label="班次时间">
            <el-date-picker
              v-model="state.form.banciDate"
              :clearable="false"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班次名称" required>
            <el-select
              v-model="state.form.name"
              placeholder="名称"
              style="width: 100%"
            >
              <el-option
                v-for="banciName in state.banciNameSelections"
                :key="banciName.id"
                :label="banciName.name"
                :value="banciName.name"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="comfirmShangGong">确定上工</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { localStorageConfig } from '@/config/localstorage.config'
  import { getBanciApi } from '@/api/pb/index'
  import dayjs from 'dayjs'
  import { shanggongApi } from '@/api/equip/equip_api'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const dialogVisible = ref(false)

  const equipData = ref()
  const banciData = ref()

  const state = reactive({
    form: {},
    banciNameSelections: [
      { id: 0, name: '白班' },
      { id: 1, name: '夜班' },
    ],
  })

  const openDialog = async (value) => {
    const load = $baseLoading()
    try {
      dialogVisible.value = true
      equipData.value = value
      let params = { pkCorp: pkCorp }
      const { code, msg, data } = await getBanciApi(params)
      console.log(data)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        banciData.value = data
        state.form.banciDate = data.banciDate
        state.form.name = data.name
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const comfirmShangGong = async () => {
    const load = $baseLoading()
    try {
      let dy = dayjs(state.form.banciDate).date()
      let mth = dayjs(state.form.banciDate).month()
      let params = {
        pkEqlist: equipData.value.pkEqlist,
        name: state.form.name,
        day: dy,
        month: mth + 1,
        banciDate: state.form.banciDate,
      }
      const { code, msg } = await shanggongApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        dialogVisible.value = false
        _emit('success-callback')
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
